﻿using System.Windows.Forms;
using System;
using SharepointSolutionInstaller.Utils.SolutionDeployment;
using SharepointSolutionInstaller.Utils.Configuration;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;


namespace SharepointSolutionInstaller.Views
{


    public partial class DeploymentTargetsView : InstallerControl
    {


        #region Constants
        public const string FormattedSiteCollectionCountWarningMessage =
          "There are {0} site collections within this web application.\n" +
          "Showing a lot of site collections may take significant time.\n" +
          "If you want to deploy to all site collections in this web application\n" +
          "you can simply check the web application checkbox without expanding it,\n" +
          "however this will still take considerable time during the install process.\n\n" +
          "Are you sure you want to show all {0} site collections for this web application?";

        public const int SiteCollectionCountWarning = 100;

        #endregion

        #region Constructor

        public DeploymentTargetsView()
        {
            InitializeComponent();
            siteCollectionsTreeView.AfterCheck += new TreeViewEventHandler(SiteCollectionsTreeView_AfterCheck);
            this.Load += new EventHandler(Control_Load);
            officeDeploymentCheckBox.Click += new EventHandler(officeDeploymentCheckBox_Click);
            this.Title = Resources.CommonUIStrings.controlTitleSiteDeployment;
            this.SubTitle = Resources.CommonUIStrings.controlSubTitleSiteDeployment;
            OfficeDeployment();
        }

        void officeDeploymentCheckBox_Click(object sender, EventArgs e)
        {
            installOfficeSolution = officeDeploymentCheckBox.Checked;
        }

        #endregion

        #region overrides

        protected internal override void Close(InstallOptions options)
        {
            foreach (TreeNode webAppTreeNode in siteCollectionsTreeView.Nodes)
            {
                // Add the web application as a target
                if (webAppTreeNode.Checked)
                {
                    WebApplicationInfo webAppInfo = webAppTreeNode.Tag as WebApplicationInfo;
                    if (webAppInfo != null)
                    {
                        options.WebApplicationTargets.Add(webAppInfo.Application);

                        ExtendedTreeNode extendedWebAppTreeNode = webAppTreeNode as ExtendedTreeNode;
                        if (extendedWebAppTreeNode != null)
                        {
                            if (!extendedWebAppTreeNode.Populated)
                            {
                                // Add ALL site collections within the web app as targets
                                foreach (SPSite siteCollection in webAppInfo.Application.Sites)
                                {
                                    options.SiteCollectionTargets.Add(siteCollection);
                                }
                            }
                            else
                            {
                                // Add the checked site collections within this web application as targets 
                                foreach (TreeNode siteCollTreeNode in webAppTreeNode.Nodes)
                                {
                                    if (siteCollTreeNode.Checked)
                                    {
                                        SiteCollectionInfo siteCollInfo = siteCollTreeNode.Tag as SiteCollectionInfo;
                                        if (siteCollInfo != null)
                                        {
                                            options.SiteCollectionTargets.Add(siteCollInfo.SiteCollection);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region Event Handlers

        private void Control_Load(object sender, EventArgs e)
        {
            ConfigureSiteCollectionTreeView();
            EnableDisableNextButton();
            Form.contentControls.Add(new InstallProcess());
        }

        void SiteCollectionsTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            EnableDisableNextButton();
        }

        void WebAppTreeNode_TreeNodePopulate(object sender, TreeViewCancelEventArgs e)
        {
            WebApplicationInfo webAppInfo = e.Node.Tag as WebApplicationInfo;
            if (webAppInfo != null)
            {
                if (webAppInfo.Application.Sites.Count >= SiteCollectionCountWarning)
                {
                    string msg = String.Format(FormattedSiteCollectionCountWarningMessage, webAppInfo.Application.Sites.Count);
                    if (MessageBox.Show(msg, "Large Number of Site Collections", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                    {
                        e.Cancel = true;
                    }
                }
                if (!e.Cancel)
                {
                    foreach (SPSite site in webAppInfo.Application.Sites)
                    {
                        SiteCollectionInfo siteCollInfo = new SiteCollectionInfo(site, false);
                        ExtendedTreeNode.AddNewExtendedTreeNode(e.Node.Nodes, siteCollInfo.ToString(), siteCollInfo);
                    }
                }
            }
        }

        #endregion

        #region Private Methods

        private void OfficeDeployment()
        {
            if (!isOfficeSolutionFileExist)
            {
                officeDeploymentCheckBox.Enabled = false;
            }
        }

        private void EnableDisableNextButton()
        {
            bool enabled = false;
            foreach (TreeNode webAppTreeNode in siteCollectionsTreeView.Nodes)
            {
                ExtendedTreeNode extendedWebAppTreeNode = webAppTreeNode as ExtendedTreeNode;
                if (extendedWebAppTreeNode != null && extendedWebAppTreeNode.Checked && !extendedWebAppTreeNode.Populated)
                {
                    enabled = true;
                    break;
                }
                else
                {
                    foreach (TreeNode siteCollTreeNode in webAppTreeNode.Nodes)
                    {
                        if (siteCollTreeNode.Checked)
                        {
                            enabled = true;
                            break;
                        }
                    }
                }
            }

            Form.btnNext.Enabled = enabled;
        }

        private void ConfigureSiteCollectionTreeView()
        {
            PopulateTreeViewForWebApps(siteCollectionsTreeView.Nodes, SPWebService.AdministrationService.WebApplications);
            PopulateTreeViewForWebApps(siteCollectionsTreeView.Nodes, SPWebService.ContentService.WebApplications);
        }

        private void PopulateTreeViewForWebApps(TreeNodeCollection treeNodes, SPWebApplicationCollection webApps)
        {
            foreach (SPWebApplication application in webApps)
            {
                WebApplicationInfo webAppInfo = new WebApplicationInfo(application, false);
                ExtendedTreeNode webAppTreeNode = ExtendedTreeNode.AddNewExtendedTreeNode(treeNodes, webAppInfo.ToString(), true, webAppInfo);
                webAppTreeNode.TreeNodePopulate += new ExtendedTreeNode.TreeNodeEventHandler(WebAppTreeNode_TreeNodePopulate);
            }
        }

        #endregion

    }
}